package com.team.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.entity.PageParameter;
import com.team.entity.Registry;
import com.team.mapper.PatientMapper;
import com.team.mapper.RegisInfoMapper;
import com.team.service.RegistryService;
import com.team.mapper.RegistryMapper;
import com.team.util.ParamForm;
import com.team.util.RegisInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author asus
* @description 针对表【registry(挂号单表)】的数据库操作Service实现
* @createDate 2024-07-13 14:39:20
*/
@Service
public class RegistryServiceImpl extends ServiceImpl<RegistryMapper, Registry>
    implements RegistryService{
    @Autowired
    private RegistryMapper registryMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private RegisInfoMapper regisInfoMapper;

    @Override
    public int addRegistry(Registry registry) {
        return registryMapper.insert(registry);
    }

    @Override
    public PageInfo<RegisInfo> getAllRegistryByParam(ParamForm paramForm) {
        QueryWrapper<Registry> queryWrapper=new QueryWrapper<>();
        //是否输入身份证
        if (paramForm.getIdCard()!=null&&!paramForm.getIdCard().equals("")){
            Integer id= patientMapper.getPatientId(paramForm.getIdCard());
            //通过身份证查询id，再通过id查询挂号单
            queryWrapper.eq("patient_id",id);
        }
        if (paramForm.getDepartmentId()!=null&&!paramForm.getDepartmentId().equals("")){
            queryWrapper.eq("department_id",paramForm.getDepartmentId());
        }
        if (paramForm.getUserId()!=null&&!paramForm.getUserId().equals("")){
            queryWrapper.eq("user_id",paramForm.getUserId());
        }
        if (paramForm.getStatus()!=null&&!paramForm.getStatus().equals("")){
            queryWrapper.eq("status",paramForm.getStatus());
        }
        List<Registry> list1=registryMapper.selectList(queryWrapper);
        PageHelper.startPage(paramForm.getPage(),paramForm.getPageSize());
        List<Registry> list=registryMapper.selectList(queryWrapper);
        System.out.println("list----"+list.size());
        List<RegisInfo> regisInfoList=regisInfoMapper.getAllRegisInfo();
        System.out.println("regisInfoList---"+regisInfoList.size());

        List<Long> ids=new ArrayList<>();
        for (Registry r:list){
            ids.add(r.getId());
            System.out.print(r.getId()+"-");
        }
        List<RegisInfo> newRegisInfoList=new ArrayList<>();
        System.out.println("-"+ids.size());
        for (RegisInfo ri:regisInfoList){
            System.out.print(ri.getId()+"-");
            if (ids.contains(ri.getId())){
                newRegisInfoList.add(ri);
            }
        }
        System.out.println("---------"+newRegisInfoList.size());
        System.out.println("----"+regisInfoList.size());

        PageInfo<RegisInfo> pageInfo=new PageInfo<>(newRegisInfoList);
        pageInfo.setTotal(list1.size());
        return pageInfo;
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        return registryMapper.updateStatus(id, status);
    }

}




